package com.health.ts.controller.attachment;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.attachment.Attachment;
import com.health.ts.service.attachment.AttachmentService;

/**  
* @ClassName: attachmentController  
* @Description:附件管理
*/  
@Controller
@RequestMapping(value = "/api/attachment",produces= BaseController.JSON_UTF8)
public class AttachmentController extends BaseController{
	
	@Autowired
	AttachmentService attachmentService;
	
	/**
	 * 附件上传
	 * @param business_id
	 * @param file
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/upload/{business_id}", method=RequestMethod.POST)
	@ResponseBody
	public ApiResult upload(@PathVariable String business_id,HttpServletRequest request) throws Exception {
		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
		attachmentService.upload(business_id, files);
		return ApiResult.success();
	 }
	
	/**
	 * 根据业务ID返回附件列表
	 * @param business_id
	 * @return
	 */
	@RequestMapping(value="/{business_id}", method=RequestMethod.GET)
	@ResponseBody
	public ApiResult listAttachments(@PathVariable String business_id) {
		List<Attachment> attachs= attachmentService.listAttachments(business_id);
		return ApiResult.success(attachs);
	}
	
	/**
	 * 根据附件ID删除附件
	 * @param business_id
	 * @return
	 */
	@RequestMapping(value="/delete/{id}", method=RequestMethod.DELETE)
	@ResponseBody
	public ApiResult deleteAttachment(@PathVariable String id) {
		attachmentService.deleteAttachment(id);
		return ApiResult.success();
	}
	
	/**
	 * 根据附件ID下载附件
	 * @param business_id
	 * @return
	 */
	@RequestMapping(value="/download/{id}", method=RequestMethod.GET)
	public ResponseEntity<byte[]> download(@PathVariable String id) throws Exception {
		Map<String,Object> attachment=	attachmentService.getAttachment(id);
		String fileName=attachment.get("fileName").toString();
		byte[] attachBytes =(byte[]) attachment.get("bytes");
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(attachBytes, headers, HttpStatus.OK);
	}
	
	/**
	 * 患者知情同意书下载附件
	 * @param business_id
	 * @return
	 */
	@RequestMapping(value="/agreeBook/download", method=RequestMethod.GET)
	public ResponseEntity<byte[]> download() throws Exception {
		Map<String,Object> attachment=	attachmentService.getAgreeBook();
		String fileName=attachment.get("fileName").toString();
		byte[] attachBytes =(byte[]) attachment.get("bytes");
		HttpHeaders headers = new HttpHeaders();
		headers.setContentDispositionFormData("attachment", new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(attachBytes, headers, HttpStatus.OK);
	}
}
